namespace CSharpPro._1013;

public class Demo
{
    public void TestPrint1()
    {
        //作业一
             static int[] GetThreeValidIntegers()
            {
                int[] numbers = new int[3];
                for (int i = 0; i < 3; i++)
                {
                    Console.Write($"请输入第{i + 1}个整数：");
                    
                    while (!int.TryParse(Console.ReadLine(), out numbers[i]))
                    {
                        Console.Write("输入错误！请重新输入整数：");
                    }
                }
                return numbers;
            }

            
             static void BubbleSortAscending(int[] array)
            {
                if (array == null || array.Length < 2) return; // 无需排序的情况
                for (int i = 0; i < array.Length - 1; i++)
                {
                    for (int j = 0; j < array.Length - 1 - i; j++)
                    {
                        
                        if (array[j] > array[j + 1])
                        {
                            int temp = array[j];
                            array[j] = array[j + 1];
                            array[j + 1] = temp;
                        }
                    }
                }
            }

           
             static void PrintSortedResult(int[] sortedArray)
            {
                Console.WriteLine("从小到大排序结果：" + string.Join(" → ", sortedArray));
            }
        }

    public void TestPrint2()
    {
        //作业2
            static bool IsValidNumber(int number)
            {
                bool notMultipleOf7 = number % 7 != 0;
                bool notContain7 = !number.ToString().Contains("7");
        
                return notMultipleOf7 && notContain7; 
            }

           
            static int PrintAndSumValidNumbers(int start, int end)
            {
                int sum = 0;
                Console.WriteLine($"1到100之间符合条件的数字：");
        
                for (int i = start; i <= end; i++)
                {
                    if (IsValidNumber(i)) 
                    {
                        Console.Write(i + " ");
                        sum += i; 
                    }
                }
                return sum;
            }
        }

    public void TestPrint3()
    {
        //作业3
            
                for (int i = 1; i <= 9; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        Console.Write($"{j}×{i}={j*i,2} ");
                    }
                    Console.WriteLine(); 
                }
        }

    public void TestPrint4()
    {
        //作业4
        static int CalculateProperDivisorsSum(int number)
        {
             int sum = 1; 
               for (int i = 2; i * i <= number; i++)
                {
                    if (number % i == 0)
                    {
                        sum += i;
                        if (i != number / i)
                        {
                            sum += number / i;
                        }
                    }
                }
                return number == 1 ? 0 : sum;
            }

            static bool IsPerfectNumber(int number)
            {
                if (number < 2)
                    return false;
                return CalculateProperDivisorsSum(number) == number;
            }
            
            static void FindAndPrintPerfectNumbers(int maxRange)
            {
                for (int i = 2; i <= maxRange; i++)
                {
                    if (IsPerfectNumber(i))
                    {
                        Console.Write($"完数：{i}，其真因子为：1");
                        for (int j = 2; j <= i / 2; j++)
                        {
                            if (i % j == 0)
                                Console.Write($", {j}");
                        }
                        Console.WriteLine();
                    }
                }
            }
        }

    public void TestPrint5()
    {
        //作业5
            static int GetValidPositiveInteger()
            {
                int num;
                Console.Write("请输入一个正整数：");
                
                while (!int.TryParse(Console.ReadLine(), out num) || num <= 0)
                {
                    Console.Write("输入无效！请重新输入一个正整数：");
                }
                return num;
            }

            static int GetDigitCount(int number)
            {
                if (number == 0) return 1; 
        
                int count = 0;
                while (number > 0)
                {
                    number /= 10;
                }
                return count;
            }

            static void PrintDigitsReversed(int number)
            {
                while (number > 0)
                {
                    int lastDigit = number % 10; 
                    Console.Write(lastDigit + " "); 
                    number /= 10; 
                }
                Console.WriteLine();
            }
        }

    public void TestPrint6()
    {
        //作业6
            static int GetValidDiamondRows()
            {
                int rows;
                Console.Write("请输入菱形的行数（需为正奇数）：");
        
                while (!int.TryParse(Console.ReadLine(), out rows) 
                       || rows <= 0 
                       || rows % 2 == 0)
                {
                    Console.Write("输入无效！请重新输入一个正奇数：");
                }
                return rows;
            }
            
            static void PrintDiamond(int totalRows)
            {
                int midRow = totalRows / 2;
                for (int i = 0; i < totalRows; i++)
                {
                    int spaceCount = Math.Abs(midRow - i);
                    
                    int starCount = 2 * (midRow - spaceCount) + 1;
                    
                    for (int j = 0; j < spaceCount; j++)
                    {
                        Console.Write(" ");
                    }
                    for (int k = 0; k < starCount; k++)
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
    
    
    
    
    

